package com.jkoss.wine.statistics.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.ExcelUtiles;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.statistics.entity.Orders;
import com.jkoss.wine.statistics.service.IOrdersService;
import com.jkoss.wine.statistics.vo.OrdersVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;

/**
 *  前端控制器
 * 
 * @Author bg2it
 * @Version 1.0, 2019-08-12
 * @See
 * @Since com.jkoss.wine.statistics.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/statistics/orders")
public class OrdersController extends BaseController {


	@Autowired
	private IOrdersService iOrdersService;
	
	@RequestMapping("/list")
	@RequiresPermissions("/statistics/orders/list")
	public String list(Date startTime, Date endTime, String employeeName, String shop, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("employeeName", employeeName);
		dwzPageBean.getCountResultMap().put("shop", shop);
		dwzPageBean.getCountResultMap().put("startTime", startTime);
		dwzPageBean.getCountResultMap().put("endTime", endTime);
		Wrapper wrapper = Condition.create().ne("o.del",1).like("se.employee_name",employeeName).like("s.shop_name",shop).eq("o.order_type",1);
		if (!CommonUtil.isBlank(startTime)){
			wrapper.ge("o.pay_time",startTime);
		}
		if (!CommonUtil.isBlank(endTime)){
			wrapper.le("o.pay_time",endTime);
		}
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			//默认排序字段
			wrapper.orderBy("o.add_time", false);
		}
		Page resultPage = iOrdersService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "statistics/orders/list";
	}

	@RequestMapping("/excel")
	@RequiresPermissions("/statistics/orders/excel")
	public void excel(Date startTime, Date endTime, String employeeName, String shop, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().ne("o.del",1).like("se.employee_name",employeeName).like("s.shop_name",shop).eq("o.order_type",1);
		if (!CommonUtil.isBlank(startTime)){
			wrapper.ge("o.pay_time",startTime);
		}
		if (!CommonUtil.isBlank(endTime)){
			wrapper.le("o.pay_time",endTime);
		}
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			//默认排序字段
			wrapper.orderBy("o.add_time", false);
		}
		ExcelUtiles.exportExcel(iOrdersService.selectVo(wrapper), "门店流水", "流水", OrdersVo.class, "门店流水.xls", response);
	}
	

}

